
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(SysUser::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysUser::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(
                        ColumnDef::new(SysUser::Username)
                            .string()
                            .string_len(100)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(SysUser::Password)
                            .string()
                            .string_len(100)
                            .not_null(),
                    )
                    .col(ColumnDef::new(SysUser::RealName).string().string_len(100))
                    .col(ColumnDef::new(SysUser::Avatar).string().string_len(100))
                    .col(ColumnDef::new(SysUser::Gender).integer())
                    .col(ColumnDef::new(SysUser::Email).string().string_len(100))
                    .col(ColumnDef::new(SysUser::Mobile).string().string_len(100))
                    .col(ColumnDef::new(SysUser::OrgId).big_integer())
                    .col(ColumnDef::new(SysUser::LeaderId).big_integer())
                    .col(ColumnDef::new(SysUser::SuperAdmin).integer().default(0))
                    .col(ColumnDef::new(SysUser::Status).integer().default(1))
                    .col(ColumnDef::new(SysUser::TenantId).big_integer())
                    .col(ColumnDef::new(SysUser::Creator).big_integer())
                    .col(
                        ColumnDef::new(SysUser::CreatedAt)
                            .timestamp_with_time_zone()
                            .not_null(),
                    )
                    .col(ColumnDef::new(SysUser::Updater).big_integer())
                    .col(ColumnDef::new(SysUser::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysUser::Version).integer().default(0))
                    .col(ColumnDef::new(SysUser::Deleted).integer().default(0))
                    .to_owned(),
            )
            .await?;

        // 构建 INSERT 语句，对普通字段使用类型安全方式
        let now = Expr::current_timestamp(); //
        let insert = sea_query::Query::insert()
            .into_table(SysUser::Table)
            .columns([
                SysUser::Id,
                SysUser::Username,
                SysUser::Password,
                SysUser::RealName,
                SysUser::Avatar,
                SysUser::Gender,
                SysUser::Email,
                SysUser::Mobile,
                SysUser::Status,
                SysUser::SuperAdmin,
                SysUser::TenantId,
                SysUser::Version,
                SysUser::Deleted,
                SysUser::Creator,
                SysUser::CreatedAt,
                SysUser::Updater,
                SysUser::UpdatedAt,
            ])
            .values([
                10000.into(),
                "admin".into(),
                "dc1fd00e3eeeb940ff46f457bf97d66ba7fcc36e0b20802383de142860e76ae6".into(),
                "admin".into(),
                "https://cdn.geoinfo.net/images/avatar.png".into(),
                0.into(),
                "babamu@126.com".into(),
                "13612345678".into(),
                1.into(),
                1.into(),     // super_admin
                10000.into(), // tenant_id
                0.into(),     // version
                0.into(),     // deleted
                10000.into(), // creator
                now.clone().into(),
                10000.into(), // updater
                now.clone().into(),
            ])
            .unwrap()
            // 手动添加 create_time 和 update_time 使用数据库函数
            .to_owned();

        manager.exec_stmt(insert).await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(SysUser::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum SysUser {
    Table,
    Id,
    Username,
    Password,
    RealName,
    Avatar,
    Gender,
    Email,
    Mobile,
    OrgId,
    LeaderId,
    SuperAdmin,
    Status,
    TenantId,
    Creator,
    CreatedAt,
    Updater,
    UpdatedAt,
    Version,
    Deleted,
}
